﻿using ImageGenerator.Helpers;
using Wpf.Ui;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using Clipboard = System.Windows.Clipboard;

namespace ImageGenerator.ViewModels.Pages
{
    public partial class SettingsViewModel : ObservableObject, INavigationAware
    {
        private readonly ISnackbarService snackbarService;

        [ObservableProperty]
        private string _hardwareId = "";
        [ObservableProperty]
        private Visibility _invalid = Visibility.Collapsed;
        [ObservableProperty]
        private Visibility _valid = Visibility.Collapsed;
        [ObservableProperty]
        private string _expirationDateInfo = "";
        [ObservableProperty]
        private string _expirationDate = "";

        public SettingsViewModel(ISnackbarService snackbarService, LicenseHelper lic)
        {
            this.snackbarService = snackbarService;
            HardwareId = lic.HardwareId;
            if (lic.Validation.Item1 == true)
                Valid = Visibility.Visible;
            else
                Invalid = Visibility.Visible;

            if (lic.Validation.Item2 != null){
                ExpirationDate = lic.Validation.Item2.Value.ToString("yyyy-MM-dd 00:00:00");
                ExpirationDateInfo = "认证有效期至 " + lic.Validation.Item2.Value.ToString("yyyy-MM-dd 00:00:00");
            }
        }

        private bool _isInitialized = false;

        [ObservableProperty]
        private string _appVersion = string.Empty;

        [ObservableProperty]
        private ApplicationTheme _currentTheme = ApplicationTheme.Unknown;

        public void OnNavigatedTo()
        {
            if (!_isInitialized)
                InitializeViewModel();
        }

        public void OnNavigatedFrom() { }

        private void InitializeViewModel()
        {
            CurrentTheme = ApplicationThemeManager.GetAppTheme();
            AppVersion = $"当前版本 - {GetAssemblyVersion()}";

            _isInitialized = true;
        }

        private string GetAssemblyVersion()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString()
                ?? string.Empty;
        }

        [RelayCommand]
        private void OnChangeTheme(string parameter)
        {
            switch (parameter)
            {
                case "theme_light":
                    if (CurrentTheme == ApplicationTheme.Light)
                        break;

                    ApplicationThemeManager.Apply(ApplicationTheme.Light);
                    CurrentTheme = ApplicationTheme.Light;

                    break;

                default:
                    if (CurrentTheme == ApplicationTheme.Dark)
                        break;

                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    CurrentTheme = ApplicationTheme.Dark;
                    break;
            }
        }

        [RelayCommand]
        private void OnCopy2Clipboard()
        {
            Clipboard.SetDataObject(HardwareId);
            snackbarService.Show("提示", "机器码已复制到剪切板，请发送给管理员激活", ControlAppearance.Info, new SymbolIcon(SymbolRegular.Info24), TimeSpan.FromSeconds(5));
        }
    }
}
